package com.dragon.modules.yuegou.service.impl.coupon;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.coupon.CouponTemplateStockEntity;
import com.dragon.modules.yuegou.entity.coupon.CouponTemplateStockRecordEntity;
import com.dragon.modules.yuegou.enums.code.CouponCode;
import com.dragon.modules.yuegou.enums.coupon.CouponTemplateStockRecordStockType;
import com.dragon.modules.yuegou.enums.coupon.CouponTemplateStockRecordType;
import com.dragon.modules.yuegou.mapper.coupon.CouponTemplateStockRecordMapper;
import com.dragon.modules.yuegou.model.coupon.CouponTemplateStockRecord;
import com.dragon.modules.yuegou.query.coupon.CouponTemplateStockQuery;
import com.dragon.modules.yuegou.service.coupon.CouponTemplateStockRecordService;
import com.dragon.modules.yuegou.service.coupon.CouponTemplateStockService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 券库存变更记录 服务实现
 *
 * @author pbuilder generated
 * @date 2020-06-30 17:39:39
 * @version 2.0
 */
@Service("coupCouponTemplateStockRecordService")
public class CouponTemplateStockRecordServiceImpl
		extends ServiceImpl<CouponTemplateStockRecordMapper, CouponTemplateStockRecordEntity>
		implements CouponTemplateStockRecordService {

	@Autowired
	private CouponTemplateStockService couponTemplateStockService;
	
	@Override
	@Transactional
	public void editStock(String id, Integer type, Integer amount) {
		//修改类型:0-减少;1-新增
		Check.notEmpty(id, "模板Id不能为空");
		Check.notNull(type, "修改类型不能为空");
		Check.notNull(amount, "优惠劵数量不能为空");
		//查询模板库存
		CouponTemplateStockEntity stock = couponTemplateStockService.getById(id);
		CouponTemplateStockRecord stockRecord = new CouponTemplateStockRecord();
		if (type == 0) {
			//校验减少数量是否超过库存剩余数
			if (amount > stock.getStockSurplus()) {
				throw new DragonException(CouponCode.OUTBOUND_GREATER_INVENTORY);
			}
			//剩余库存减出库数量
			stock.setStockSurplus(stock.getStockSurplus() - amount);
			//已用库存加出库数量
			stock.setStockUse(stock.getStockUse() + amount);
			//新增出出库记录
			stockRecord.setStockType(CouponTemplateStockRecordStockType.OUT);
		}if (type == 1) {
			//总库存
			stock.setStockTotal(stock.getStockTotal() + amount);
			//库存剩余
			stock.setStockSurplus(stock.getStockSurplus() + amount);
			//新增出出库记录
			stockRecord.setStockType(CouponTemplateStockRecordStockType.IN);
		}
		//修改库存信息
		CouponTemplateStockQuery query = new CouponTemplateStockQuery();
		query.setId(stock.getId());
		query.setVersion(stock.getVersion());
		stock.setVersion(stock.getVersion() + 1);
		couponTemplateStockService.update(stock, query.toQueryWrapper());
		//添加出入库记录
		stockRecord.setCouponTemplateId(stock.getId());
		stockRecord.setTotal(stock.getStockTotal());
		stockRecord.setStock(stock.getStockSurplus());
		stockRecord.setChange(amount);
		stockRecord.setType(CouponTemplateStockRecordType.EDIT);
		baseMapper.insert(stockRecord);
	}

}